<template>
	<view class='page_cont'>
		<view class='title'>Hello~</view>
		<view class='subtitle'>欢迎来到家巴啦教师直聘</view>
		<!-- #ifdef MP-WEIXIN -->
		<button class='login_btn' open-type="getUserInfo" @getuserinfo="getUserInfo" v-if='!isLogin&&agree'>微信登录</button>
		<view class='login_btn' @click="show_toast()" v-if='!isLogin&&!agree'>微信登录</view>
		<button class='login_btn' open-type="getPhoneNumber" @getphonenumber="getPhonenumber" v-if="isLogin&&agree">绑定手机</button>
		<!-- #endif -->
		<!-- #ifdef APP-PLUS -->
		<view class='login_btn' @click="app_login()">{{step?"微信登陆":"账号登录"}}</view>
		<!-- #endif -->
		<view class='pwd_btn' @click="nav_login()" v-if="step">密码登录</view>
		<view class='agr_wrap' @click="change_agree()">
		    <image class='icon' :src="agree?'/static/icon46.png':'/static/icon45.png'"></image>
		    <view class='text'>同意家巴啦<text class='txt' @click.stop="nav_go(0)">《用户协议》</text>和<text class='txt' @click.stop="nav_go(1)">《隐私政策》</text></view>
		</view>
		<!-- #ifdef APP-PLUS -->
		<!-- <view class='apple_wrap' v-if="platform=='ios'">
			<view class='point_wrap'>
				<view class="line"></view>
				<view class='text'>其他登录方式</view>
				<view class="line"></view>
			</view>
			<view class="btn_wrap">
				<view class='apple_btn' @click="apple_login()">
					<image class="icon" src="/static/icon71.png"></image>
					<view class="text">Sign in with Apple</view>
				</view>
			</view>
		</view> -->
		<!-- #endif -->
	</view>
</template>

<script>
	import config from "../../api/config.js"
	import api from "../../api/api.js"
	import util from "../../utils/util.js"
	import resume from "../../api/myResume.js"
	export default{
		data(){
			return{
				isLogin: false,
				agree: true,
				bind: '',
				openId: '',
				unionId: '',
				userinfo: null,
				user_address: '',
				bindtype: 1,
				platform: '',
				appleLogin: false,
				step: null
			}
		},
		onLoad() {
			var that=this;
			that.user_address=uni.getStorageSync("user_address");
			// #ifdef APP-PLUS
			that.step=uni.getStorageSync("step")
			uni.getSystemInfo({
			    success: function (res) {
					// console.log(res)
					that.platform=res.platform
					var system=Number(res.system.split(".")[0])
					if(system>=13){
						that.appleLogin=true
					}
				},
			})
			// #endif
		},
		methods:{
			// app微信登陆
			app_login(){
				var that=this;
				if(!that.step){
					uni.redirectTo({
						url: '../psdLogin/psdLogin'
					})
					return false;
				}
				if(!that.agree){
					that.show_toast()
					return false;
				}
				// that.bindtype=2;
				uni.login({
					provider: 'weixin',
					success: function (loginRes) {
						// console.log(loginRes.authResult);
						var info=loginRes.authResult;
						that.openId=info.openid
						that.unionId=info.unionid
						info.openId=info.openid
						info.unionId=info.unionid
						uni.setStorageSync("openId",info.openId)
						uni.setStorageSync("unionId",info)
						//
						that.is_bind_wx(info)
						// 获取用户信息
						uni.getUserInfo({
							provider: 'weixin',
							success: function (infoRes) {
								// console.log(infoRes)
								that.userinfo=infoRes.userInfo
								uni.setStorageSync("user_info",infoRes.userInfo)
							}
						})
					}
				})
			},
			// apple登录
			apple_login(){
				var that=this;
				if(!that.appleLogin){
					util.page_show_toast("系统版本低于13.0,无法提供授权登录",3000)
					return false;
				}
				uni.login({
				    provider: 'apple',
				    success: function (loginRes) {
						// console.log(loginRes)
				        // 登录成功  
				        uni.getUserInfo({
				            provider: 'apple',  
				            success(res) {
				                // 获取用户信息成功  
								// console.log(res)
								var appleId=res.userInfo.openId
								that.apple_bind(appleId)
				            }
				        })
				    },
				    fail: function (err) {  
				        // 登录失败  
						util.page_show_toast("登录失败,请稍后重试",3000)
				    }  
				});
			},
			// 获取本地手机号
			// getPhone(){
			// 	console.log(plus.os.name)
			// 	switch(plus.os.name){  
			// 		case 'Android':
			// 			// 程序全局环境对象    
			// 			let mainActivity = plus.android.runtimeMainActivity();    
			// 			let Context = new plus.android.importClass("android.content.Context");    
			// 			let TelephonyManager = new plus.android.importClass("android.telephony.TelephonyManager");    
			// 			let tm = mainActivity.getSystemService(Context.TELEPHONY_SERVICE);    
			// 			let number = tm.getLine1Number();  
			// 			uni.showToast({  
			// 			  icon: 'none',  
			// 			  title: number  
			// 			})  
			// 			break;  
			// 	}
			// },
			// 授权用户信息
			getUserInfo(e){
				// console.log(e)
			    var that=this;
				that.userinfo=e.detail.userInfo
			    if(e.detail.errMsg=="getUserInfo:ok"){
			      // 解密
			      that.userinfo_decrypt_wx(e.detail)
			    }
			},
			// 授权绑定手机
			getPhonenumber(e){
			    var that=this,info=e.detail;
			    if(info.encryptedData){
					that.phone_decrypt_wx(info)
			    }else{
					// uni.navigateTo({
					// 	url: '../bindMobile/bindMobile',
					// })
			    }
			},
			// 提示
			show_toast(){
			    util.page_show_toast('请先勾选同意家巴啦《用户协议》和《隐私政策》',3000)
			},
			// 密码登录
			nav_login(){
				uni.redirectTo({
					url: '../psdLogin/psdLogin'
				})
			},
			// 同意
			change_agree(){
				this.agree=!this.agree;
			},
			// 协议
			nav_go(index){
			    uni.navigateTo({
					url: '../../mypage/webView/webView?index='+index
			    })
			},
			// unionId解密
			userinfo_decrypt_wx(info){
			    var params={
					"openId": uni.getStorageSync("openId"),
					"encryptedData": info.encryptedData,
					"iv": info.iv
			    }
			    config.decrypt_wx(params).then(res=>{
					// console.log(res)
			        var info=res.data.data;
					this.openId=info.openId;
					this.unionId=info.unionId;
					uni.setStorageSync('unionId', info)
			        // 是否绑定微信
			        this.is_bind_wx(info)
			    })
			},
			// 判断是否绑定微信
			is_bind_wx(info){
				var params={
					"unionId": info.unionId,
					"openId": info.openId,
					"bindType": this.bindtype,
					"thirdAppId": api.data.appId
				}
				config.is_bind_wx(params).then(res=>{
					// console.log(res.data.data);
					this.bind=res.data.data;
					if(res.data.data){
						this.wx_login()
					}else{
						// #ifdef MP-WEIXIN
						this.isLogin=true;
						// #endif
						// #ifdef APP-PLUS
						var type=this.bind?1:2
						uni.redirectTo({
							url: "/pages/bindMobile/bindMobile?type="+type
						})
						// #endif
					}
				})
			},
			// 判断appleId
			apple_bind(appleId){
				config.apple_bind({"unionId":appleId}).then(res=>{
					// console.log(res)
					if(res.data.data){
						var user=res.data.data;
						util.page_show_toast('登录成功',1500);
						uni.setStorageSync('userinfo', user.userInfo)
						uni.setStorageSync('token', user.token)
						this.isFirStLogin()
					}else{
						util.page_show_toast("用户不存在，请注册",2000)
						setTimeout(()=>{
							uni.redirectTo({
								url: "/pages/bindMobile/bindMobile?type="+3+'&appleId='+appleId
							})
						},2000)
						
					}
				})
			},
			// 微信登录
			wx_login(){
				var params={
					"unionId": this.unionId,
					"openId": this.openId,
					"bindType": this.bindtype,
					"thirdAppId": api.data.appId
				}
				config.wx_login(params).then(res=>{
					// console.log(res);
					var user=res.data.data;
					if(user.userInfo.phone){
						util.page_show_toast('登录成功',1500);
						uni.setStorageSync('userinfo', user.userInfo)
						uni.setStorageSync('token', user.token)
						uni.removeStorageSync("user_info")
						this.isFirStLogin()
					}else{
						// #ifdef MP-WEIXIN
						this.isLogin=true;
						// #endif
						// #ifdef APP-PLUS
						var type=this.bind?1:2
						uni.redirectTo({
							url: "/pages/bindMobile/bindMobile?type="+type
						})
						// #endif
					}
				})
			},
			isFirStLogin(){
				config.isFirstLogin().then(res=>{
					// console.log(res)
					// if(res.data.data.accountBusinesses==null&&res.data.data.accountProfile==null){
					// 	uni.navigateTo({
					// 		url:'../bindIdentity/bindIdentity'
					// 	})
					// }else {
					// 	uni.switchTab({
					// 		url:'../index/index'
					// 	})
					// }
					if (res.data.data.accountProfile == null && res.data.data.accountBusinesses == null) {
						// 
						this.get_org_list()
					} else if(res.data.data.accountProfile!==null && res.data.data.accountProfile.isLogin===1){
						uni.setStorageSync("identityNum", 1)
						setTimeout(function() {
							uni.navigateBack({
								delta: 1,
								fail(){
									uni.switchTab({
										url: '/pages/index/index'
									})
								}
							})
						}, 1500)
						this.saveLoginStatus(res.data.data.accountProfile.id,2)
					}else if(res.data.data.accountBusinesses!==null && res.data.data.accountBusinesses.isLogin===1){
						uni.setStorageSync("identityNum", 2)
						uni.setStorageSync("userIdentity",res.data.data.accountBusinesses)
						setTimeout(function() {
							uni.navigateBack({
								delta: 1,
								fail(){
									uni.switchTab({
										url: '/pages/index/index'
									})
								}
							})
						}, 1500)
						this.saveLoginStatus(res.data.data.accountBusinesses.id,1)
					}
				})
			},
			// 是否有认证机构
			get_org_list(){
				var params={
					"phone": uni.getStorageSync("userinfo").phone
				}
				config.get_org_list(params).then(res=>{
					var list=res.data.data;
					if(list&&list.length>0){
						var len=list.length;
						if(list[len-1].status==1){
							// 审核
							uni.redirectTo({
								url: '/pages/mechVerify/mechVerify?type=3&status=2'
							})
						}else if(list[len-1].status==2){
							// 审核通过
							uni.redirectTo({
								url: '/pages/mechVerify/mechVerify?type=2&status=2'+'&id='+list[len-1].businessId
							})
						}else if(list[len-1].status==4){
							// 审核未通过
							uni.redirectTo({
								url: '/pages/mechVerify/mechVerify?type=1&status=2'+'&id='+list[len-1].businessId
							})
						}
					}else{
						uni.navigateTo({
							url: '../bindIdentity/bindIdentity'
						})
					}
				})
			},
			// 手机号解密
			phone_decrypt_wx(info){
			    var params={
					"openId": this.openId,
					"encryptedData": info.encryptedData,
					"iv": info.iv
			    }
			    config.decrypt_wx(params).then(res=>{
			        var phone=res.data.data.phoneNumber;
			        if(this.bind){
			          this.wx_bind_phone(phone)
			        }else{
			          this.wx_bind(phone)
			        }
			    })
			},
			// 绑定手机
			wx_bind_phone(phone){
			    var params={
			      "phone": phone,
			      "unionId": this.unionId,
			    }
			    config.wx_bind_phone(params).then(res=>{
			        if(res.data.data){
						this.wx_login();
			        }else{
						util.page_show_toast('绑定出错，请稍后再试',2000)
			        }
			    })
			},
			// 绑定手机号和微信
			wx_bind(phone){
			    var params={
					"nickName": this.userinfo.nickName?this.userinfo.nickName:phone,
					"phone": phone,
					"avatar": this.userinfo.avatarUrl?this.userinfo.avatarUrl:'https://ossdev0.jiabala.com/aeQCeISaVcGXw',
					"sex": this.userinfo.gender,
					"unionId": this.unionId,
					"openId": this.openId,
					"bindType": this.bindtype,
					"thirdAppId": api.data.appId,
					"province": this.user_address?this.user_address.province:'',
					"city": this.user_address?this.user_address.city:'',
					"area": this.user_address?this.user_address.district:'',
					"town": this.user_address?this.user_address.town:'',
					"address": this.user_address?this.user_address.address:''
			    }
			    config.wx_bind(params).then(res=>{
					 // console.log(res);
					// 登录
			        this.wx_login();
			    })
			},
			// 记录下次状态
			saveLoginStatus(id,identity){
				resume.save_login_identity({id:id,identity:identity}).then(res=>{
					// console.log(res);
				})
			}
		}
	}
</script>

<style lang="scss">
	.page_cont{
		width: 100%;
		padding-top: 80rpx;
		.title{
			text-align: center;
			height: 84rpx;
			font-size: 60rpx;
			font-weight: 400;
			line-height: 84rpx;
			color: #3F4A58;
			margin-bottom: 6rpx;
		},
		.subtitle{
			text-align: center;
			height: 66rpx;
			font-size: 48rpx;
			font-weight: 400;
			line-height: 66rpx;
			color: #3F4A58;
		}
		.login_btn{
			width: 654rpx;
			height: 104rpx;
			line-height: 104rpx;
			text-align: center;
			font-size: 36rpx;
			border-radius: 16rpx;
			background: #5B90FF;
			color: #fff;
			font-weight:600;
			letter-spacing:2rpx;
			padding: 0;
			margin: 170rpx auto 32rpx auto;
			border: 0;
			outline: 0;
			&::after{
				border: 0;
			}
		}
		.pwd_btn{
		  width: 654rpx;
		  text-align: right;
		  margin: 0 auto 32rpx auto;
		  height: 40rpx;
		  font-size: 28rpx;
		  font-weight:400;
		  line-height: 40rpx;
		  color: #5B90FF;
		}
		.agr_wrap{
			margin: 0 138rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			.icon{
				width: 28rpx;
				height: 28rpx;
				margin-right: 12rpx;
				margin-top: 2rpx;
			}
			.text{
				flex: 1;
				height: 34rpx;
				font-size: 24rpx;
				font-weight:400;
				line-height: 34rpx;
				color:rgba(146,155,167,0.5);
				.txt{
					color: #929BA7;
				}
			}
		}
		.apple_wrap{
			position: fixed;
			bottom: 60rpx;
			left: 0;
			right: 0;
			width: 80%;
			margin: auto;
			.point_wrap{
				display: flex;
				flex-direction: row;
				align-items: center;
				margin-bottom: 32rpx;
				.line{
					flex: 1;
					height: 2rpx;
					background: rgba(63,74,88,0.4);
				}
				.text{
					height: 32rpx;
					line-height: 32rpx;
					font-size: 24rpx;
					font-weight: 400;
					margin: 0 24rpx;
					color: rgba(63,74,88,0.8);
				}
			}
			.btn_wrap{
				display: inline-block;
				margin: auto;
				.apple_btn{
					background: #000;
					padding: 24rpx 128rpx;
					display: flex;
					flex-direction: row;
					align-items: center;
					border-radius: 8rpx;
					.icon{
						width: 28rpx;
						height: 28rpx;
						margin-right: 16rpx;
						margin-top: -4rpx;
					}
					.text{
						height: 32rpx;
						line-height: 32rpx;
						font-size: 28rpx;
						font-weight: 500;
						color: #fff;
					}
				}
			}
		}
	}
</style>
